<?php

/**
 * 登陆控制器
 *
 * @author haifeng.wu
 */

require 'CommonController.php';

class LoginController extends CommonController
{
    /**
     * 初始化
     * 
     * @return void
     */
    public function init()
    {
        $this->_loginIsCheck = 0;
        parent::init();
    }
   
    /**
     * 登录
     * 
     * @return void
     */
    public function indexAction()
    {
        //如果已经登录则返回主页
        if (Application_Model_UserLogin::isLogined()) {
            redirect($this->view->projectUrl(array('controller'=>'index', 'action'=>'index')));
        }

        if ($this->_request->isPost()) {
            $userName = trim($this->_request->getPost('userName'));
            $password = trim($this->_request->getPost('password'));
            if (empty($userName) || empty($password)){
                $this->view->message('用户名和密码不能为空，请返回填写。');
            } else {
                $userLoginModel = new Application_Model_UserLogin();
                $loginStatus = $userLoginModel->login($userName, $password);
                if ($loginStatus == 10) {
                    $this->view->message('用户名和密码错误，请返回填写。');
                } else if ($loginStatus == 20) {
                    $this->view->message('你的账户被锁定，如有问题请与管理员联系。');
                } else {
                    redirect($this->view->projectUrl(array('controller'=>'index', 'action'=>'index')));
                }
            }
        }
    }
    
    /**
     * 退出
     * 
     * @return void
     */
    public function logoutAction()
    {
        $userLoginModel = new Application_Model_UserLogin();
        $userLoginModel->logout();
        redirect($this->view->projectUrl(array('action'=>'index')));
        exit;
    }
}